package com.emex.app.stuff.gui;

import com.emex.app.shared.data.Cros;
import com.emex.app.shared.data.Position;
import com.emex.app.stuff.gui.model.PositionTableModel;
import com.emex.app.stuff.provider.ShopDataProvider;
import com.emex.app.stuff.utils.StringUtilities;
import com.emex.app.stuff.utils.UIUtils;
import java.util.List;

/**
 *
 * @author Sergey Karavaev
 */
public class SearchAnalogDialog extends javax.swing.JDialog {

    private PositionTableModel analogsTableModel;

    private Position originalPosition;

    private Position analogPosition;

    /**
     * Creates new form SearchAnalogDialog
     */
    public SearchAnalogDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public PositionTableModel getAnalogsTableModel() {
        return analogsTableModel;
    }

    public void setAnalogsTableModel(PositionTableModel analogsTableModel) {
        this.analogsTableModel = analogsTableModel;
    }

    public Position getOriginalPosition() {
        return originalPosition;
    }

    public void setOriginalPosition(Position originalPosition) {
        this.originalPosition = originalPosition;
    }

    public Position getAnalogPosition() {
        return analogPosition;
    }

    public void setAnalogPosition(Position analogPosition) {
        this.analogPosition = analogPosition;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchField = new org.jdesktop.swingx.JXSearchField();
        jLabel3 = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        resultLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.title")); // NOI18N

        searchField.setText(org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.searchField.text")); // NOI18N
        searchField.setSearchMode(org.jdesktop.swingx.JXSearchField.SearchMode.REGULAR);
        searchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchFieldActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.addButton.text")); // NOI18N
        addButton.setEnabled(false);
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(resultLabel, org.openide.util.NbBundle.getMessage(SearchAnalogDialog.class, "SearchAnalogDialog.resultLabel.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 159, Short.MAX_VALUE)
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addComponent(jLabel3)
                    .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resultLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        if (null != originalPosition && null != analogPosition) {
            Cros cros = new Cros();
            cros.setMainId(originalPosition.getId());
            cros.setAnalogId(analogPosition.getId());
            cros = ShopDataProvider.getInstance().insertCros(cros);
            analogsTableModel.dataAdded(analogPosition);
        }

        setVisible(false);
    }//GEN-LAST:event_addButtonActionPerformed

    private void searchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchFieldActionPerformed
        String code = searchField.getText();
        if (null != code && !code.isEmpty()) {
            String formattedCode = StringUtilities.formatPositionCode(StringUtilities.formatStringFromSpecialCharacters(code)).toUpperCase();
            searchField.setText(formattedCode);
            List<Position> positions = ShopDataProvider.getInstance().getPositionByCode(formattedCode);
            if (null == positions || positions.isEmpty()) {
                UIUtils.showInfoMessage("Позиция не найдена");
                addButton.setEnabled(false);
                resultLabel.setText("Результат: ничего не найдено");
            } else {
                analogPosition = positions.get(0);
                addButton.setEnabled(true);
                this.getRootPane().setDefaultButton(addButton);
                resultLabel.setText("Результат: " + analogPosition.getName());
            }
        }
    }//GEN-LAST:event_searchFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel resultLabel;
    private org.jdesktop.swingx.JXSearchField searchField;
    // End of variables declaration//GEN-END:variables
}
